package cn.iocoder.yudao.module.enterprise.controller.admin.enterpriseCurriculumVitae;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.enterprise.controller.admin.enterpriseCurriculumVitae.vo.*;
import cn.iocoder.yudao.module.enterprise.convert.enterpriseCurriculumVitae.EnterpriseCurriculumVitaeConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.enterpriseCurriculumVitae.EnterpriseCurriculumVitaeDO;
import cn.iocoder.yudao.module.enterprise.service.enterpriseCurriculumVitae.EnterpriseCurriculumVitaeService;
import cn.iocoder.yudao.module.member.api.stundent.AdminStudentApi;
import cn.iocoder.yudao.module.member.api.stundent.dto.StudentEducationalExperienceRespDTO;
import cn.iocoder.yudao.module.member.api.stundent.dto.StudentInfoRespDTO;
import cn.iocoder.yudao.module.member.api.stundent.dto.StudentPageReqDTO;
import cn.iocoder.yudao.module.member.api.stundent.dto.StudentRespDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 -企业端- 用户简历")
@RestController
@RequestMapping("/enterprise/curriculum-vitae")
@Validated
public class EnterpriseCurriculumVitaeController {

    @Resource
    private EnterpriseCurriculumVitaeService curriculumVitaeService;
    @Resource
    private AdminStudentApi adminStudentApi;
    @PostMapping("/create")
    @ApiOperation("创建用户简历")
    @PreAuthenticated
    public CommonResult<Long> createCurriculumVitae(@Valid @RequestBody EnterpriseCurriculumVitaeCreateReqVO createReqVO) {
        return success(curriculumVitaeService.createCurriculumVitae(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户简历")
    @PreAuthenticated
    public CommonResult<Boolean> updateCurriculumVitae(@Valid @RequestBody EnterpriseCurriculumVitaeUpdateReqVO updateReqVO) {
        curriculumVitaeService.updateCurriculumVitae(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户简历")
    @PreAuthenticated
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deleteCurriculumVitae(@RequestParam("id") Long id) {
        curriculumVitaeService.deleteCurriculumVitae(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户简历")
    @PreAuthenticated
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<EnterpriseCurriculumVitaeRespVO> getCurriculumVitae(@RequestParam("id") Long id,Long recordId) {
        EnterpriseCurriculumVitaeDO curriculumVitae = curriculumVitaeService.getCurriculumVitae(id,recordId);
        return success(EnterpriseCurriculumVitaeConvert.INSTANCE.convert(curriculumVitae));
    }

    @GetMapping("/getNosend")
    @ApiOperation("获得用户简历")
    @PreAuthenticated
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<EnterpriseCurriculumVitaeRespVO> getCurriculumVitae(@RequestParam("id") Long id) {
        EnterpriseCurriculumVitaeDO curriculumVitae = curriculumVitaeService.getCurriculumVitae(id);
        StudentRespDTO studentRespDTO= adminStudentApi.getStudent(curriculumVitae.getStudentId());
        curriculumVitae.setInJobTime(studentRespDTO.getInJobTime());
        curriculumVitae.setJobStatus(studentRespDTO.getJobStatus());
        return success(EnterpriseCurriculumVitaeConvert.INSTANCE.convert(curriculumVitae));
    }

    @GetMapping("/list")
    @ApiOperation("获得用户简历列表")
    @PreAuthenticated
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<EnterpriseCurriculumVitaeRespVO>> getCurriculumVitaeList(@RequestParam("ids") Collection<Long> ids) {
        List<EnterpriseCurriculumVitaeDO> list = curriculumVitaeService.getCurriculumVitaeList(ids);
        return success(EnterpriseCurriculumVitaeConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/page")
    @ApiOperation("获得用户简历分页")
    @PreAuthenticated
    public CommonResult<PageResult<EnterpriseCurriculumVitaeRespVO>> getCurriculumVitaePage(@Valid @RequestBody EnterpriseCurriculumVitaePageReqVO pageVO) {
        PageResult<EnterpriseCurriculumVitaeDO> pageResult = curriculumVitaeService.getCurriculumVitaePage(pageVO);
        return success(EnterpriseCurriculumVitaeConvert.INSTANCE.convertPage(pageResult));
    }

    @PostMapping("/getEnterpriseCurriculumVitaePage")
    @ApiOperation("企业端-自主招聘列表")
    @PreAuthenticated
    public CommonResult<PageResult<EnterpriseCurriculumVitaeRespVO>> getEnterpriseCurriculumVitaePage(@Valid @RequestBody EnterpriseCurriculumVitaePageReqVO pageVO) {
        pageVO.setRelationType(4);
        PageResult<EnterpriseCurriculumVitaeDO> pageResult = curriculumVitaeService.getEnterpriseCurriculumVitaePage(pageVO);
        return success(EnterpriseCurriculumVitaeConvert.INSTANCE.convertPage(pageResult));
    }

    @PostMapping("/getEnterpriseStudentPage")
    @ApiOperation("企业端-人才夹-学生列表")
    @TenantIgnore
    public CommonResult<PageResult<StudentInfoRespDTO>> getEnterpriseStudentPage(@Valid @RequestBody EnterpriseCurriculumVitaePageReqVO pageVO) {
        pageVO.setRelationType(5);
        StudentPageReqDTO studentPage=new StudentPageReqDTO();
        BeanUtils.copyProperties(pageVO,studentPage);
        PageResult<StudentInfoRespDTO> pageResult = adminStudentApi.getEnterpriseStudentPage(studentPage);
        return success( pageResult);
    }



    @PostMapping("/getStudentEducationalExperienceByStudentId")
    @ApiOperation("运营-人才夹-根据学生ID查看教育经历")
    public CommonResult<List<StudentEducationalExperienceRespDTO>> getStudentEducationalExperienceByStudentId(@NotNull(message = "学生ID不能为空") Long studentId) {
        List<StudentEducationalExperienceRespDTO> list = adminStudentApi.getStudentEducationalExperienceByStudentId(studentId);
        return success(list);
    }


    @GetMapping("/export-excel")
    @ApiOperation("导出用户简历 Excel")
    @OperateLog(type = EXPORT)
    public void exportCurriculumVitaeExcel(@Valid EnterpriseCurriculumVitaeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<EnterpriseCurriculumVitaeDO> list = curriculumVitaeService.getCurriculumVitaeList(exportReqVO);
        // 导出 Excel
        List<EnterpriseCurriculumVitaeExcelVO> datas = EnterpriseCurriculumVitaeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户简历.xls", "数据", EnterpriseCurriculumVitaeExcelVO.class, datas);
    }

}
